<?php
/**
 * Created by PhpStorm.
 * User: clcw908
 * Date: 2017/1/11
 * Time: 11:47
 */
// 回车手续老数据处理
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
	die('error');
}

if (!defined('API_ROOT_PATH')) {
	define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
/**
 * @return MysqliDb
 */
function init_db()
{
	$db = new MysqliDb ([
		'host' => MYSQLDB,
		'username' => MYSQLDB_USER,
		'password' => MYSQLDB_PWD,
		'db' => MYSQLDB_DATABASE,
		'port' => 3306,
		'prefix' => 'au_',
		'charset' => 'utf8'
	]);
	return $db;
}
$db = init_db();

$id = 0;
$lastId = 0;

while (true){
	$list = $db->where('procedures_upload','','<>')->where('id',$lastId,'>')->get('car_return_log',2000);
	//print_r($db->getLastQuery());die();
	if(empty($list)){
		logUtil('处理完成');die();
	}
	foreach ($list as $key =>$value){

		$orderId = $value['order_id'];
		$sid = $value['sid'];
		$proceduresUpload = $value['procedures_upload'];
		$proceduresArray = explode('、',$proceduresUpload);
		//print_r($proceduresArray);die();
		if(count($proceduresArray) > 1){
			foreach ($proceduresArray as $k => $v){
				$db->startTransaction();
				switch ($v){
					case '全套扫描':
						scanAll($proceduresArray,"全套扫描",$orderId,$sid);
						break;
					case '全套照片':
						photoAll($proceduresArray,"全套照片",$orderId,$sid);
						break;
					case '扫描车架号合影':
						scanVinNo($proceduresArray,'扫描车架号合影',$orderId,$sid);
						break;
					case '照片车架号合影':
						photoVinNo($proceduresArray,'照片车架号合影',$orderId,$sid);
						break;
					case '登记证3': //登记证3、4页
						registrationCertificate('登记证3、4页',$orderId,$sid);
						break;
				}
				$db->commit();
				//die();
			}
		}else{
			$procedure = $proceduresArray[0];
			switch ($procedure){
				case '全套扫描':
					scanAll($procedure,"全套扫描",$orderId,$sid);
					break;
				case '全套照片':
					photoAll($procedure,"全套照片",$orderId,$sid);
					break;
				case '扫描车架号合影':
					scanVinNo($procedure,'扫描车架号合影',$orderId,$sid);
					break;
				case '照片车架号合影':
					photoVinNo($procedure,'照片车架号合影',$orderId,$sid);
					break;
				case '登记证3': //登记证3、4页
					registrationCertificate('登记证3、4页',$orderId,$sid);
					break;
			}
		}
		$lastId = $value['id'];
	}

}

//全套扫描
function scanAll($allRequirement,$requirementName,$orderId,$sid){
	global $db;
	$certificateIds = [5,6,7,8];
	if($requirementName == '全套扫描'){
		$requirement = $db->where('order_id',$orderId)
			->where('certificate_id',$certificateIds,'in')
			//->where('certificate_image_type',2)
			->get('procedures_requirement');
		//print_r($db->getLastQuery());die();
		if(empty($requirement)){
			$arr = [
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>5,
					'certificate_name'=>'变更后行驶证',
					'certificate_image_type'=>2,//表示 扫描件
					'transfer_status' => 2, //过户后
				],
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>6,
					'certificate_name'=>'变更后登记证',
					'certificate_image_type'=>2,//表示 扫描件
					'transfer_status' => 2, //过户后
				],
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>7,
					'certificate_name'=>'买方身份证',
					'certificate_image_type'=>2,//表示 扫描件
					'transfer_status' => 2, //过户后
				],
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>8,
					'certificate_name'=>'过户票',
					'certificate_image_type'=>2,//表示 扫描件
					'transfer_status' => 2, //过户后
				],
			];
			$result = $db->insertMulti('procedures_requirement',$arr);
			if(!$result){
				logUtil('拍单:'.$orderId.'生成全套扫描手续要求失败!');die();
			}else{
				logUtil('拍单:'.$orderId.'生成全套扫描手续要求成功!');
			}
		}else{
			$result = in_array('全套照片',$allRequirement);
			if($result){
				$data = [
					'certificate_image_type' => 3,
				];
				$result = $db->where('order_id',$orderId)
					->where('certificate_id',$certificateIds,'in')
					->update('procedures_requirement',$data);
				/*$arr = [
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>5,
						'certificate_name'=>'变更后行驶证',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>6,
						'certificate_name'=>'变更后登记证',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>7,
						'certificate_name'=>'买方身份证',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>8,
						'certificate_name'=>'过户票',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
				];
				if(!empty($requirement)){ //如果有手续要求信息
					$delResult = $db->where('order_id',$orderId)->where('certificate_id',$certificateIds,'in')->delete('procedures_requirement');
					if($delResult){
						logUtil('拍单:'.$orderId.'删除全套扫描和全套照片手续要求成功!');
					}else{
						logUtil('拍单:'.$orderId.'删除全套扫描和全套照片手续要求失败!');die();
					}
				}
				$result = $db->insert('procedures_requirement',$arr);*/
				if(!$result){
					logUtil('拍单:'.$orderId.'生成全套扫描和全套照片手续要求失败!');die();
				}else{
					logUtil('拍单:'.$orderId.'生成全套扫描和全套照片手续要求成功!');
				}
			}
		}
	}
}

//全套照片
function photoAll($allRequirement,$requirementName,$orderId,$sid){
	global $db;
	$certificateIds = [5,6,7,8];
	if($requirementName == '全套照片'){
		$requirement = $db->where('order_id',$orderId)
			->where('certificate_id',$certificateIds,'in')
			//->where('certificate_image_type',1)
			->get('procedures_requirement'); //查询图片的
		if(empty($requirement)){
			$arr = [
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>5,
					'certificate_name'=>'变更后行驶证',
					'certificate_image_type'=>1,//表示 照片
					'transfer_status' => 2, //过户后
				],
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>6,
					'certificate_name'=>'变更后登记证',
					'certificate_image_type'=>1,//表示 照片
					'transfer_status' => 2, //过户后
				],
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>7,
					'certificate_name'=>'买方身份证',
					'certificate_image_type'=>1,//表示 照片
					'transfer_status' => 2, //过户后
				],
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>8,
					'certificate_name'=>'过户票',
					'certificate_image_type'=>1,//表示 照片
					'transfer_status' => 2, //过户后
				],
			];

			$result = $db->insertMulti('procedures_requirement',$arr);
			if(!$result){
				logUtil('拍单:'.$orderId.'生成全套照片手续要求失败!');die();
			}else{
				logUtil('拍单:'.$orderId.'生成全套照片手续要求成功!');
			}
		}else {
			$result = in_array('全套扫描', $allRequirement);
			if ($result) {
				$data = [
					'certificate_image_type' => 3,
				];
				$result = $db->where('order_id', $orderId)
					->where('certificate_id', $certificateIds, 'in')
					->update('procedures_requirement', $data);
				/*$arr = [
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>5,
						'certificate_name'=>'变更后行驶证',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>6,
						'certificate_name'=>'变更后登记证',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>7,
						'certificate_name'=>'买方身份证',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
					[
						'sid' =>$sid,
						'order_id' => $orderId,
						'certificate_id'=>8,
						'certificate_name'=>'过户票',
						'certificate_image_type'=>3,//表示 既有扫描件，又有照片
						'transfer_status' => 2, //过户后
					],
				];*/

				/*if(!empty($requirement)){ //如果有手续要求信息
					$delResult = $db->where('order_id',$orderId)->where('certificate_id',$certificateIds,'in')->delete('procedures_requirement');
					if($delResult){
						logUtil('拍单:'.$orderId.'删除全套扫描和全套照片手续要求成功!');
					}else{
						logUtil('拍单:'.$orderId.'删除全套扫描和全套照片手续要求失败!');die();
					}
				}*/
				//$result = $db->insert('procedures_requirement',$arr);
				if (!$result) {
					logUtil('拍单:' . $orderId . '生成全套扫描和全套照片手续要求失败!');
					die();
				} else {
					logUtil('拍单:' . $orderId . '生成全套扫描和全套照片手续要求成功!');
				}
			}
		}
	}
}

// 车架号 扫描件
function scanVinNo($allRequirement,$requirementName,$orderId,$sid){
	global $db;
	$certificateIds = [9];
	if($requirementName == '扫描车架号合影'){
		$requirement = $db->where('order_id',$orderId)
			->where('certificate_id',$certificateIds,'in')
			//->where('certificate_image_type',2)
			->get('procedures_requirement');
		if(empty($requirement)){
			$arr = [
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>9,
					'certificate_name'=>'扫描车架号合影',
					'certificate_image_type'=>2,//表示 扫描件
					'transfer_status' => 2, //过户后
				],
			];

			$result = $db->insertMulti('procedures_requirement',$arr);
			if(!$result){
				logUtil('拍单:'.$orderId.'生成扫描车架号合影手续要求失败!');die();
			}else{
				logUtil('拍单:'.$orderId.'生成扫描车架号合影手续要求成功!');
			}
		}else {
			$result = in_array('照片车架号合影', $allRequirement);
			if ($result) {

				$data = [
					'certificate_image_type' => 3,
				];
				$result = $db->where('order_id', $orderId)
					->where('certificate_id', $certificateIds, 'in')
					->update('procedures_requirement', $data);

				if (!$result) {
					logUtil('拍单:' . $orderId . '生成扫描车架号合影和照片车架号合影手续要求失败!');
					die();
				} else {
					logUtil('拍单:' . $orderId . '生成扫描车架号合影和照片车架号合影手续要求成功!');
				}
			}
		}

	}
}

//照片 扫描件
function photoVinNo($allRequirement,$requirementName,$orderId,$sid){
	global $db;
	$certificateIds = [9];
	if($requirementName == '照片车架号合影'){
		$requirement = $db->where('order_id',$orderId)
			->where('certificate_id',$certificateIds,'in')
			//->where('certificate_image_type',1)
			->get('procedures_requirement');
		if(empty($requirement)){
			$arr = [
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>9,
					'certificate_name'=>'扫描车架号合影',
					'certificate_image_type'=>1,//表示 照片
					'transfer_status' => 2, //过户后
				],
			];

			$result = $db->insertMulti('procedures_requirement',$arr);
			if(!$result){
				logUtil('拍单:'.$orderId.'生成照片车架号合影手续要求失败!');die();
			}else{
				logUtil('拍单:'.$orderId.'生成照片车架号合影手续要求成功!');
			}
		}else{
			$result = in_array('扫描车架号合影',$allRequirement);
			if($result){
				$data = [
					'certificate_image_type' => 3,
				];
				if(empty($requirement)){

				}
				$result = $db->where('order_id',$orderId)
					->where('certificate_id',$certificateIds,'in')
					->update('procedures_requirement',$data);

				if(!$result){
					logUtil('拍单:'.$orderId.'生成照片车架号合影和扫描车架号合影手续要求失败!');die();
				}else{
					logUtil('拍单:'.$orderId.'生成照片车架号合影和扫描车架号合影手续要求成功!');
				}
			}

		}
	}
}

//登记证3、4页
function registrationCertificate($requirementName,$orderId,$sid){
	global $db;
	$certificateIds = [6];
	if($requirementName == '登记证3、4页'){
		$requirement = $db->where('order_id',$orderId)
			->where('certificate_id',$certificateIds,'in')
			//->where('certificate_image_type',1)
			->get('procedures_requirement');
		if(empty($requirement)){
			$arr = [
				[
					'sid' =>$sid,
					'order_id' => $orderId,
					'certificate_id'=>6,
					'certificate_name'=>'登记证3、4页',
					'certificate_image_type'=>1,//表示 照片
					'transfer_status' => 2, //过户后
				],
			];

			$result = $db->insertMulti('procedures_requirement',$arr);
			if(!$result){
				logUtil('拍单:'.$orderId.'生成登记证3、4页手续要求失败!');die();
			}else{
				logUtil('拍单:'.$orderId.'生成登记证3、4页手续要求成功!');
			}
		}
	}
}